/**
 * Copyright © 2024, ZhiYun Network Co.,Ltd. All rights reserved.
 * ZhiYun PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.itcoon.demo.web.controller.transform;

import com.itcoon.cloud.framework.transform.Transformer;
import com.itcoon.cloud.framework.webmvc.response.ApiResponse;
import com.itcoon.demo.web.controller.transform.pojo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName:com.itcoon.demo.web.controller.transform.TransformController
 * Description:
 *
 * Date: 10/6/24 2:31 PM
 *
 * @author: miaoxia.yu
 *
 * @version 1.0.0
 */
@Tag(name = "TRANSFORM")
@RequestMapping("/transforms")
@RestController
public class TransformController {

    @GetMapping("/string")
    @Operation(summary = "转换String")
    public ApiResponse<String> simpleTransform(String s) {
        String result = Transformer.<String, String>to(String.class).apply(s)
                .convert();
        return ApiResponse.ok(result);
    }

    @PostMapping("/simple")
    @Operation(summary = "简单对象")
    public ApiResponse<EmployeeVO> simpleTransform(@RequestBody EmployeeDO employee) {
        EmployeeVO result = Transformer.to(EmployeeVO.class)
                .apply(employee, "id")
                .convert();
        return ApiResponse.ok(result);
    }

    @PostMapping("/list")
    @Operation(summary = "简单List")
    public ApiResponse<List<EmployeeVO>> listTransform(@RequestBody List<EmployeeDO> employees) {
        List<EmployeeVO> result = Transformer.<EmployeeDO, EmployeeVO>toList(EmployeeVO.class)
                .apply(employees)
                .itemMap((employee)-> Transformer.<EmployeeDO, EmployeeVO>to(EmployeeVO.class).apply(employee).setAt("name", EmployeeDO::getName))
                .convert();
        return ApiResponse.ok(result);
    }

    @PostMapping("/emed")
    @Operation(summary = "嵌套对象")
    public ApiResponse<EmployeeVO> emeTransform(@RequestBody EmployeeExtendDO employee) {
        EmployeeVO result = Transformer.to(EmployeeVO.class).apply(employee, "employeeLevel.id").convert();
        return ApiResponse.ok(result);
    }

    @PostMapping("/setAt")
    @Operation(summary = "简单对象")
    public ApiResponse<EmployeeVO> setAt(@RequestBody EmployeeExtendDO employee) {
        EmployeeVO result = Transformer
                .to(EmployeeVO.class)
                .apply(employee)
                .convert();

        return ApiResponse.ok(result);
    }


    @GetMapping("stringToLocalDateTime")
    @Operation(summary = "string转DateTime")
    public ApiResponse<LocalDateTime> stringToDateTime(@RequestParam String dateTime){
        LocalDateTime localDateTime = Transformer.<String, LocalDateTime>to(LocalDateTime.class).apply(dateTime).convert();
        return ApiResponse.ok(localDateTime);
    }

    @GetMapping("longToLong")
    @Operation(summary = "基本类型")
    public ApiResponse<Long> longToLong(@RequestParam long value){
        return ApiResponse.ok(Transformer.to(Long.class).apply(value).convert());
    }


    @PostMapping("array")
    @Operation(summary = "数组")
    public ApiResponse<ArrayRespVO> array(@RequestBody ArrayReqVO command){
        return ApiResponse.ok(Transformer.to(ArrayRespVO.class).apply(command).convert());
    }

    @PostMapping("/parameterized")
    @Operation(summary = "参数化")
    public ApiResponse<ParameterizedDemo<String>> parameterized(@RequestBody Demo demo){
        return ApiResponse.ok(Transformer.<Demo, ParameterizedDemo>to(ParameterizedDemo.class, String.class).apply(demo).convert());
    }

}
